from django.contrib.auth.models import User
from rest_framework import serializers

from User.models import SiteUser, Resource
from misaka.serializers.Serializers import NestedModelSerializer, getUserRequestSerializer


class SiteUserRegisterSerializer(serializers.ModelSerializer):
    """
    注册使用的SiteUser注册序列器
    """
    password = serializers.CharField(write_only=True)  # 只写不读

    class Meta:
        model = SiteUser
        fields = ['username', 'password', 'nickname']

    def create(self, validated_data):
        user = SiteUser(
            username=validated_data['username'],
            nickname=validated_data['nickname']
        )
        user.set_password(validated_data['password'])
        user.save()
        return user


class SiteUserDetailSerializer(serializers.ModelSerializer):
    """
    注册使用的SiteUser注册序列器
    """

    class Meta:
        model = SiteUser
        fields = "__all__"


class ResourceSerializer(getUserRequestSerializer):
    user_field_name = 'user'
    class Meta:
        model = Resource
        # fields = "__all__"
        exclude = ['user']
